#!/usr/bin/env bash

# 微服务按需启动脚本
# 兼容macOS zsh环境

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# JVM参数配置
GATEWAY_JVM_ARGS="-Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m -XX:+UseG1GC"
SYSTEM_JVM_ARGS="-Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m -XX:+UseG1GC"
AUTH_JVM_ARGS="-Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m -XX:+UseG1GC"
TASK_JVM_ARGS="-Xms256m -Xmx512m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=128m -XX:+UseG1GC"
NOTIFICATION_JVM_ARGS="-Xms256m -Xmx512m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=128m -XX:+UseG1GC"

# 服务列表
declare -A SERVICES=(
    ["gateway"]="zx-services:zx-gateway-15775"
    ["system"]="zx-services:zx-system-block-15776"
    ["auth"]="zx-services:zx-auth-15778"
    ["notification-producer"]="zx-services:zx-notification-producer-15780"
    ["notification-consumer"]="zx-services:zx-notification-consumer-15781"
    ["notification-admin"]="zx-services:zx-notification-admin-15782"
    ["task"]="zx-services:zx-task-manager-15779"
)

# 显示帮助信息
show_help() {
    echo -e "${BLUE}微服务按需启动脚本${NC}"
    echo "使用方法: ./start-services.sh [选项] [服务名...]"
    echo ""
    echo "选项:"
    echo "  -h, --help     显示帮助信息"
    echo "  -l, --list     列出所有可用服务"
    echo "  -a, --all      启动所有服务"
    echo ""
    echo "示例:"
    echo "  ./start-services.sh gateway auth    # 启动网关和认证服务"
    echo "  ./start-services.sh -a                  # 启动所有服务"
    echo "  ./start-services.sh -l                  # 列出所有服务"
}

# 列出所有服务
list_services() {
    echo -e "${BLUE}可用服务列表:${NC}"
    for service in "${!SERVICES[@]}"; do
        echo -e "  ${GREEN}${service}${NC} - ${SERVICES[$service]}"
    done
}

# 启动单个服务
start_service() {
    local service_name=$1
    local service_info=${SERVICES[$service_name]}
    
    if [ -z "$service_info" ]; then
        echo -e "${RED}错误: 未知服务 $service_name${NC}"
        return 1
    fi
    
    echo -e "${YELLOW}正在启动服务: $service_name${NC}"
    
    # 分割模块和项目名
    IFS=':' read -ra ADDR <<< "$service_info"
    module=${ADDR[0]}
    project=${ADDR[1]}
    
    # 设置JVM参数
    local jvm_args=""
    case "$service_name" in
        "gateway")
            jvm_args="$GATEWAY_JVM_ARGS"
            ;;
        "system")
            jvm_args="$SYSTEM_JVM_ARGS"
            ;;
        "auth")
            jvm_args="$AUTH_JVM_ARGS"
            ;;
        "task")
            jvm_args="$TASK_JVM_ARGS"
            ;;
        "notification-producer"|"notification-consumer"|"notification-admin")
            jvm_args="$NOTIFICATION_JVM_ARGS"
            ;;
    esac
    
    # 在后台启动服务
    cd "$module" || return 1
    command mvn spring-boot:run -pl "$project" -Dspring-boot.run.profiles=dev -Djvm.args="$jvm_args" &
    cd - > /dev/null || return 1
    
    echo -e "${GREEN}服务 $service_name 已在后台启动${NC}"
}

# 启动所有服务
start_all_services() {
    echo -e "${YELLOW}启动所有服务...${NC}"
    # 按正确的依赖顺序启动核心服务
    start_service "gateway"
    sleep 2
    start_service "system"  # 系统服务需要在认证服务之前启动
    sleep 2
    start_service "auth"    # 认证服务依赖系统服务提供用户信息
    sleep 2
    
    # 启动其他服务
    start_service "notification-producer"
    sleep 2
    start_service "notification-consumer"
    sleep 2
    start_service "notification-admin"
    sleep 2
    start_service "task"
}

# 主逻辑
main() {
    # 检查参数
    if [ $# -eq 0 ]; then
        show_help
        exit 0
    fi
    
    # 处理选项
    case "$1" in
        -h|--help)
            show_help
            exit 0
            ;;
        -l|--list)
            list_services
            exit 0
            ;;
        -a|--all)
            start_all_services
            exit 0
            ;;
        -*)
            echo -e "${RED}错误: 未知选项 $1${NC}"
            show_help
            exit 1
            ;;
        *)
            # 启动指定服务
            for service in "$@"; do
                start_service "$service"
            done
            ;;
    esac
}

# 执行主逻辑
main "$@"